package com.c.ctgproject.wiget.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Bundle
import com.bumptech.glide.Glide
import com.c.ctgproject.R
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserver
import com.c.ctgproject.http.ObserverListener
import com.c.ctgproject.utils.MyLog
import kotlinx.android.synthetic.main.dialog_dorrkeeper.*
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.ResponseBody
import org.json.JSONObject
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.util.*


/**
 *作者:LiBW
 *创建日期: 2020/8/17
 *描述:门卫端入场添加车辆照片
 */
class DoorkeeperDialog(context: Context, val carId: String, private val uri: Uri, val callback: DoorkeeperDialogCallback) : Dialog(context) {
    private val photoName = "output_image.jpg"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_dorrkeeper)
        MyLog.d("图片Uri$uri")
        val bitmap = BitmapFactory.decodeStream(context.contentResolver.openInputStream(uri))
        val file = File(context.externalCacheDir, photoName)
        try {
            if (file.exists()) {
                file.delete()
            }
            file.createNewFile()
        } catch (e: IOException) {
            e.printStackTrace()
        }

        val bos = BufferedOutputStream(FileOutputStream(file))
        //保留图片的30%
        bitmap.compress(Bitmap.CompressFormat.JPEG, 30, bos)
        bos.flush()
        bos.close()


        Glide.with(context).load(bitmap).into(mDialogIV)
        this.setCancelable(false)
        //取消
        mCancelBTN.setOnClickListener {
            //回收bitmap
            bitmap.recycle()
            this.dismiss()
        }
        //确认
        mConfirmRL.setOnClickListener {
            val requestFile: RequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file)
            val part = MultipartBody.Part.createFormData("enterPic", file.name, requestFile)
            val fb = RequestBody.create(MediaType.parse("text/plain"), carId)
            val listener = object : ObserverListener {
                override fun onNext(t: ResponseBody) {
                    val data = t.string().toString()
                    MyLog.d("入场回调:$data")
                    val json=JSONObject(data)
                    if (json.getInt("status")==0){
                        callback.confirm()
                    }else{
                        val msg=json.getString("msg")
                        if (msg.isEmpty()){
                            callback.error("入场错误,请稍后再试")
                        }else{
                            callback.error(msg)
                        }
                    }
                    this@DoorkeeperDialog.dismiss()
                }

                override fun onError(error: String) {
                    MyLog.d("入场失败$error")
                    callback.error(error)
                    this@DoorkeeperDialog.dismiss()
                }

            }
            val hashMap = HashMap<String, String>()
            hashMap[carId] = carId
            ApiMethods.carEntrance(MyObserver(context, listener),fb , part)
            //回收bitmap
            bitmap.recycle()
        }
    }

    interface DoorkeeperDialogCallback {
        fun confirm()
        fun error(e: String)
    }
}